package com.loiot.baqi.constant;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.loiot.baqi.pojo.ZpDictionaryInfo;
import com.loiot.baqi.status.DictionaryType;
import com.loiot.baqi.utils.IndexInfoSingleTon;

/**
 * 招聘字典 类
 * 
 * @author Administrator
 *
 */
public class DictionaryUtil {

	/**
	 * 根据字典类型，获取分类列表.
	 * 
	 * @param type
	 *            字典类型
	 * @return 字典列表
	 */
	public static List<ZpDictionaryInfo> getTypes(int type) {
		Map<String, Map<String, ZpDictionaryInfo>> dictCache = getDictCache();
		List<ZpDictionaryInfo> dictList = new ArrayList<ZpDictionaryInfo>();
		Map<String, ZpDictionaryInfo> typeMap = dictCache.get(String
				.valueOf(type));
		if (typeMap == null) {
			return dictList;
		}
		Iterator<Entry<String, ZpDictionaryInfo>> ite = typeMap.entrySet()
				.iterator();
		while (ite.hasNext()) {
			dictList.add(ite.next().getValue());
		}
		return dictList;
	}

	/**
	 * 根据字典编码，获取字典名称.
	 * 
	 * @param code
	 * @return
	 */
	public static String getName(Long code) {
		ZpDictionaryInfo dict = getBean(code);
		if (dict == null) {
			return "";
		}
		return dict.getShowName();
	}

	/**
	 * 根据字典编码，获取字典信息.
	 * 
	 * @param code
	 *            字典编码
	 * @return 字典信息
	 */
	public static ZpDictionaryInfo getBean(Long code) {
		if (code == null) {
			return null;
		}
		Map<String, Map<String, ZpDictionaryInfo>> dictCache = getDictCache();
		Iterator<Entry<String, Map<String, ZpDictionaryInfo>>> iterator = dictCache
				.entrySet().iterator();
		Iterator<Entry<String, ZpDictionaryInfo>> ite = null;
		Entry<String, ZpDictionaryInfo> entry = null;
		while (iterator.hasNext()) {
			ite = iterator.next().getValue().entrySet().iterator();
			while (ite.hasNext()) {
				entry = ite.next();
				if (String.valueOf(code).equals(entry.getKey())) {
					return entry.getValue();
				}
			}
		}
		return null;
	}

	/**
	 * 获取RESUME_REGEXP字典分类.
	 * 
	 * @return RESUME_REGEXP分类列表
	 */
	public static List<ZpDictionaryInfo> getRegexpList() {
		return getTypes(DictionaryType.RESUME_REGEXP.getCode());
	}

	/**
	 * 根据字典类型和字典名称，获取字典编码.
	 * 
	 * @param type
	 *            字典类型
	 * @param name
	 *            字典名称
	 * @return 字典编码
	 */
	public static Long getCode(int type, String name) {
		if (name == null) {
			return null;
		}
		Map<String, Map<String, ZpDictionaryInfo>> dictCache = getDictCache();
		Map<String, ZpDictionaryInfo> typeMap = dictCache.get(String
				.valueOf(type));
		if (typeMap == null) {
			return null;
		}
		Iterator<Entry<String, ZpDictionaryInfo>> ite = typeMap.entrySet()
				.iterator();
		ZpDictionaryInfo dict = null;
		while (ite.hasNext()) {
			dict = ite.next().getValue();
			if (name.equals(dict.getName())) {
				return dict.getDictionaryId();
			}
		}
		return null;
	}

	/**
	 * 根据字典类型和编码，获取名称.
	 * 
	 * @param type
	 *            字典类型
	 * @param code
	 *            字典编码
	 * @return 字典名称
	 */
	public static String getName(int type, Long code) {
		if (code == null) {
			return "";
		}
		Map<String, Map<String, ZpDictionaryInfo>> dictCache = getDictCache();
		Map<String, ZpDictionaryInfo> typeMap = dictCache.get(String
				.valueOf(type));
		if (typeMap == null) {
			return "";
		}
		ZpDictionaryInfo dict = typeMap.get(String.valueOf(code));
		if (dict == null) {
			return "";
		}
		return dict.getShowName();
	}

	/**
	 * 获取字典缓存.
	 * 
	 * @return
	 */
	private static Map<String, Map<String, ZpDictionaryInfo>> getDictCache() {
		return IndexInfoSingleTon.getInstance().getDictCache();
	}

	/**
	 * 从缓存中获取所有字典.
	 * 
	 * @return 字典列表
	 */
	private static List<ZpDictionaryInfo> getDictionaries() {
		Map<String, List> maps = IndexInfoSingleTon.getInstance()
				.getIndexInfoMap();
		List<ZpDictionaryInfo> dictList = maps
				.get(Const.SESSION_DICTIONARYS_KEY);
		return dictList;
	}

}
